import 'dart:convert';

import 'package:flutter/material.dart';
import 'Home.dart';
import 'Category.dart';
import 'Member.dart';
import '../../common/ScreenAdaper.dart';
import '../../services/Storage.dart';

class Tabs extends StatefulWidget {
  @override
  _TabsState createState() => _TabsState();
}

class _TabsState extends State<Tabs> {
  int _currentIndex = 0;
  List<Widget> _pageList = [
    HomePage(),
    CategoryPage(),
    MemberPage(),
  ];
  Map _userInfo = {};
  //创建页面控制器
  var _pageController;
  @override
  void initState(){
    super.initState();
    //页面控制器初始化
    _pageController = new PageController(initialPage : _currentIndex);
    // 获取用户信息
    _getUserInfo();
  }
  _getUserInfo() async{
    try {
      Map userInfo = json.decode(await Storage.getString("userInfo"));
      if(userInfo != {}){
        setState(() {
          this._userInfo=userInfo;
        });
      }
    } catch (e){
      print(e);
    }
  }
  @override
  Widget build(BuildContext context) {
    S.init(context);
    return Scaffold(
        appBar: AppBar(
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.book, size: 28, color: Colors.black54),
              onPressed: (){
                setState(() {
                  this._currentIndex = 1;
                  //页面控制器进行跳转
                  _pageController.jumpToPage(this._currentIndex);
                });
              },
            )
          ],
          title: InkWell(
            child: Container(
              height: S.height(68),
              decoration: BoxDecoration(
                  color: Color.fromRGBO(233, 233, 233, 0.8),
                  borderRadius: BorderRadius.circular(30)
              ),
              padding: EdgeInsets.only(left: 10),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Icon(Icons.search, size: 28,color: Colors.black54),
                  Text("死人经",style: TextStyle(fontSize: S.size(28)))
                ],
              ),
            ),
            onTap: (){
              Navigator.pushNamed(context, '/search');
            },
          ),
        ),
        drawer: _drawer(),
        //必须用 PageView 加载不同的页面
        body: PageView(
          controller: _pageController,
          children: this._pageList,
          //          physics: NeverScrollableScrollPhysics(), //禁止pageView滑动
          onPageChanged: (index){
            setState(() {
              this._currentIndex=index;
            });
          },
        ),
        bottomNavigationBar: BottomNavigationBar(
          type:BottomNavigationBarType.fixed,   //配置底部tabs可以有多个按钮
          currentIndex: this._currentIndex, //配置对应的索引值选中
          fixedColor: Colors.red, // 配置选中颜色
          onTap: (int index) {
            setState(() {
              this._currentIndex=index;
              //页面控制器进行跳转
              _pageController.jumpToPage(index);
            });
          },
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
                icon: Icon(Icons.home),
                title: Text("首页")
            ),
            BottomNavigationBarItem(
                icon: Icon(Icons.category),
                title: Text("分类")
            ),
            BottomNavigationBarItem(
                icon: Icon(Icons.account_circle),
                title: Text("个人中心")
            )
          ],

        )
    );
  }

  // 侧边栏
  Drawer _drawer(){
    return Drawer(
      child:Column(
        children:[
          UserAccountsDrawerHeader(
            accountName: Text("${_userInfo["Username"] == null ? "Copy Book" : _userInfo["Username"]}",style: TextStyle(color: Colors.white),),
            accountEmail: Text("${_userInfo["Email"] == null ? "CopyBook@qq.com" : _userInfo["Email"]}",style: TextStyle(color: Colors.white),),
            currentAccountPicture:CircleAvatar(backgroundImage:NetworkImage("https://www.itying.com/images/flutter/3.png"),),// 头像
            decoration: BoxDecoration(
              //color: Colors.green,
              image: DecorationImage(image: NetworkImage("https://www.itying.com/images/flutter/2.png"),fit:BoxFit.cover),
            ),// 背景
          ),
          ListTile(
            title: Text("个人中心"),
            leading: CircleAvatar(
                child: Icon(Icons.people)
            ),
            onTap: (){
              Navigator.pushNamed(context, "/member");
            },
          ),
          ListTile(
            title: Text("退出登录"),
            leading: CircleAvatar(
                child: Icon(Icons.call_missed_outgoing)
            ),
            onTap: (){
              Storage.clear();
              Navigator.pushNamed(context, "/index");
            },
          ),
        ],
      ),
    );
  }
}